package com.github.data_manage.controller

import com.github.data_manage.common.CommonResult
import com.github.data_manage.dao.UserMessageDao
import com.github.data_manage.domain.po.UserMessage
import com.github.data_manage.util.AuthContextHolder
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.slf4j.LoggerFactory
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@Tag(name = "UserMessageController", description = "用户消息管理")
@RequestMapping("/userMessage")
class UserMessageController(
    private val userMessageDao: UserMessageDao,
    private val authContextHolder: AuthContextHolder
) {

    companion object {
        private val logger = LoggerFactory.getLogger(UserMessageController::class.java)
    }

    @GetMapping("/findAll")
    @Operation(description = "查询当前用户的所有消息")
    fun findAll(): CommonResult<List<UserMessage>> =
        CommonResult.success(userMessageDao.findAllByUserId(authContextHolder.currentUser().id!!).sortedByDescending { it.createTime })

    @PostMapping("/readAll")
    @Transactional
    @Operation(description = "将当前用户的所有消息设置为已读")
    fun readAll(): CommonResult<Boolean> {
        userMessageDao.readAllByUserId(authContextHolder.currentUser().id!!)
        return CommonResult.success(true)
    }

    @PostMapping("/deleteAll")
    @Transactional
    @Operation(description = "将当前用户的所有消息删除")
    fun deleteAll(): CommonResult<Boolean> {
        userMessageDao.removeAllByUserId(authContextHolder.currentUser().id!!)
        return CommonResult.success(true)
    }

}